package com.foreverwin.mes.production.controller;


import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.meapi.model.WorkCenter;
import com.foreverwin.mes.meapi.service.ItemService;
import com.foreverwin.mes.meapi.service.WorkCenterService;
import com.foreverwin.mes.production.model.WorkDayRestTime;
import com.foreverwin.mes.production.service.WorkDayRestTimeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/workDayRestTimes")
public class WorkDayRestTimeController {
    private final Logger logger = LoggerFactory.getLogger(WorkDayRestTimeController.class);

    @Autowired
    public WorkDayRestTimeService workDayRestTimeService;


    @Autowired
    public WorkCenterService workCenterService;

    @Autowired
    public ItemService itemService;

    /**
     * 工作日休息数据查询
     *
     * @param workCenter
     * @return
     */

    @GetMapping("workDayRestTime")
    public AjaxResult getWorkDayRestTime(String workCenter) {

        WorkCenter workCenterModel = null;
        WorkDayRestTime result = null;
        try {
            String site = CommonMethods.getSite();
            workCenterModel = workCenterService.selectById("WorkCenterBO:" + site + "," + workCenter);
            if (workCenterModel == null) {

                throw new Exception("工作中心" + workCenter + "不存在");
            }
            result = workDayRestTimeService.getWorkDayRestTimeData(site, workCenter);

        } catch (Exception e) {

            logger.error("getWorkDayRestTime -=- {}", e.toString());
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());

        }
        return AjaxResult.success(result);
    }


    /**
     * 工作日休息数据保存
     *
     * @param workCenter
     * @param startTime
     * @param endTime
     * @param modifiedDateTime
     * @return
     */
    @GetMapping("doSave")
    public AjaxResult saveWorkDayRestData(String workCenter, String startTime, String endTime, String modifiedDateTime) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        WorkDayRestTime result = new WorkDayRestTime();
        try {

            workDayRestTimeService.saveWorkDayRestData(site, user, workCenter, startTime, endTime, modifiedDateTime);

        } catch (Exception e) {

            String eStr = StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage();
            String[] eStrs = eStr.split(":");
            for (int i = 0, len = eStrs.length; i < len; i++) {

                if (eStrs[i].trim().equals("ORA-00001")) { //违反唯一约束条件
                    eStr = "该数据已维护请重新维护";
                    break;
                }
            }
            logger.error("saveWorkDayRestData -=- {}", e);
            return AjaxResult.error(eStr);
        }
        return AjaxResult.success("新增操作成功");

    }


    /**
     * 工作日休息数据更新
     *
     * @param workCenter
     * @param detailHandle
     * @param startTime
     * @param endTime
     * @param modifiedDateTime
     * @return
     */
    @GetMapping("doUpdate")
    public AjaxResult updateWorkDayRestData(String workCenter, String detailHandle, String startTime, String endTime, String modifiedDateTime) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        try {

            workDayRestTimeService.updateWorkDayRestData(site, user, workCenter, detailHandle, startTime, endTime, modifiedDateTime);
        } catch (Exception e) {

            String eStr = StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage();
            String[] eStrs = eStr.split(":");
            for (int i = 0, len = eStrs.length; i < len; i++) {

                if (eStrs[i].trim().equals("ORA-00001")) { //违反唯一约束条件
                    eStr = "该数据已维护请重新维护";
                    break;
                }
            }

            logger.error("updateWorkDayRestData -=- {}", e.toString());
            return AjaxResult.error(eStr);
        }
        return AjaxResult.success("更新操作成功");

    }


    /**
     * 删除数据
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "doDelete", method = RequestMethod.POST)
    public AjaxResult deleteWorkDayRestData(@RequestBody JSONObject jsonObject) {

        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        try {

            String workCenter = jsonObject.getString("workCenter");
            List<String> detailHandleList = (List<String>) jsonObject.get("detailHandles");
            String modifiedDateTime = jsonObject.getString("modifiedDateTime");
            workDayRestTimeService.deleteWorkDayRestData(site, user, workCenter, modifiedDateTime, detailHandleList);
        } catch (Exception e) {

            logger.error("deleteWorkDayRestData -=- {}", e.toString());
            return AjaxResult.error(StringUtils.isBlank(e.getMessage()) ? e.toString() : e.getMessage());
        }
        return AjaxResult.success("删除操作成功");

    }
}